package pl.lodz.p.browarex.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.lodz.p.browarex.base.BaseCrudService;
import pl.lodz.p.browarex.dao.OrderDAO;
import pl.lodz.p.browarex.dao.OrderProductInfoDAO;
import pl.lodz.p.browarex.model.Customer;
import pl.lodz.p.browarex.model.Order;
import pl.lodz.p.browarex.model.OrderProductInfo;
import pl.lodz.p.browarex.services.interfaces.IOrderService;

@Transactional
@Repository
public class OrderService extends BaseCrudService<OrderDAO, Order, Long>
		implements IOrderService {

	@Autowired
	public OrderService(OrderDAO dao) {
		super(dao);
	}

	@Autowired
	private OrderProductInfoDAO orderProductInfoDAO;

	@Override
	public Order getBasketOrderForCustomer(Customer customer) {
		return ((OrderDAO) this.dao)
				.getBasketOrderForCustomer(customer.getId());
	}

	@Override
	public Order getBasketOrderForCustomer(Long customerID) {
		return ((OrderDAO) this.dao).getBasketOrderForCustomer(customerID);
	}

	@Override
	public void addToOrder(Order order, OrderProductInfo orderProductInfo,
			double discount) {
		if (order.getId() != null) {
			boolean flag = true;
			order = this.dao.load(order.getId());
			for (OrderProductInfo info : order.getProductList()) {
				if (info.getId().equals(orderProductInfo.getId())) {
					flag = false;
					info.setQuantity(orderProductInfo.getQuantity());
					info.setPrice(info.getQuantity()
							* info.getProduct().getPrice() * (1.0 - discount));
					orderProductInfoDAO.save(info);
					this.dao.save(order);
					break;
				}
				if (info.getProduct().getId()
						.equals(orderProductInfo.getProduct().getId())) {
					flag = false;
					info.setQuantity(info.getQuantity()
							+ orderProductInfo.getQuantity());
					info.setPrice(info.getQuantity()
							* info.getProduct().getPrice() * (1.0 - discount));
					orderProductInfoDAO.save(info);
					this.dao.save(order);
					break;
				}
			}
			if (flag) {
				orderProductInfoDAO.save(orderProductInfo);
				List<OrderProductInfo> list = order.getProductList();
				list.add(orderProductInfo);
				order.setProductList(list);
				this.dao.save(order);
			}
		} else {
			order.setProductList(new ArrayList<OrderProductInfo>());
			orderProductInfoDAO.save(orderProductInfo);
			List<OrderProductInfo> list = order.getProductList();
			list.add(orderProductInfo);
			order.setProductList(list);
			this.dao.save(order);
		}
		// orderProductInfoDAO.save(orderProductInfo);
		// List<OrderProductInfo> list = order.getProductList();
		// list.add(orderProductInfo);
		// order.setProductList(list);
		// this.dao.save(order);
	}

	@Override
	public Order loadByOrderID(String orderID) {
		return ((OrderDAO) dao).loadByOrderID(orderID);
	}

	@Override
	public List<Order> getOrderForCustomer(Customer customer) {
		return ((OrderDAO) this.dao).getOrderForCustomer(customer.getId());
	}

	@Override
	public List<Order> getOrderForCustomer(Long customerID) {
		return ((OrderDAO) this.dao).getOrderForCustomer(customerID);
	}

}
